var ns, UIAlbumBrowser, bmLocation, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            ID: 0,
            pageindex: 0,
            pagesize: 10,
            // status: 1, // 1-任务列表 2-新增记录
            PlanType: 1,
            TaskStatus:5,//0-未开始,1-进行中,2-已结束
            planTypeList:1,
        },
        total: {
            ProcessCount: 0,
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showTop:false,
        showsearch:false,
        number:0,
        selectlist:[],
        showsearchresult:false,
        searchresult:'',
        checkbox:false,
        choosetype:{},
        choosetaskmodel:{},
        optionName:'',
        choosesortmodel:{},
        option2: [],
        option3: [
            // { text: '排序', value: 0 },
            { text: '时间近到远', value: 1 },
            { text: '时间远到近', value: 2 },
        ],
        menuType: [
            { type: 1, text: '全部类型', value: 0 },
            { type: 2, text: '设备巡检', value: 1 },
            { type: 3, text: '设备维保', value: 2 },
        ],
        OrderByType:1,
    },
    methods: {
        get_data: function () {
            var that = this;
            that.is_searching= true;
            that.can_scroll = false;
            var options = {};
            options.action = "getdeviceplantasklist";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.Status = that.form.TaskStatus;
            options.OrderByType = that.OrderByType;
            options.Type = that.form.PlanType;
            ns.post(options, function (succeed, data, err) {
                that.is_searching = false;
                if (succeed) {

                    if (data.listHandle.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.selectlist = data.listHandle;
                    } else {
                        that.selectlist = that.selectlist.concat(data.listHandle);
                    }

                    app.current_item_length = that.selectlist.length
                    // that.total.ProcessCount = data.ProcessCount;
                    // that.number=data.number;
                    // for(var i=0;i<data.list.length;i++){
                    //     data.list[i].JpushAddTime = ns.SetTime_style(data.list[i].JpushAddTime);
                    // }

                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        //获取筛选条件
        get_menuType: function () {
            var that = this;
            var options = {};
            options.action = 'getdeviceplantasksearchparams';
            options.Type = 1;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        that.option2 = data.list;
                        that.choosetaskmodel = data.list[1];
                        that.optionName = that.choosetaskmodel.Name + '(' + that.choosetaskmodel.Number + ')';
                        that.choosetype = that.menuType[0];
                        that.choosesortmodel = that.option3[0];
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        //翻页第一条数据的位置
        do_scrollbtn: function (id) {
            var sollid = document.getElementById('scroll' + id);
            window.scrollTo(0, sollid.offsetTop);
        },
        select_status: function (status) {
            var that = this;
            that.showTop = false;
            that.form.pageindex = 0;
            that.showsearchresult = true;
            if (status == 1) {
                that.searchresult = "设备巡检";
                that.form.planTypeList = [1];
            }
            if (status == 2) {
                that.searchresult = "设备维保";
                that.form.planTypeList = [2];
            }
            that.list = [];
            that.get_data();
        },
        onChooseType: function (item) {
            var that = this;
            that.choosetype = item;
            this.$refs.choosetype.toggle();
            that.form.PlanType = item.type;
            that.form.pageindex = 0;
            that.form.pagesize = 10;
            that.get_data();
        },
        onChooseTask: function (item) {
            var that = this;
            that.choosetaskmodel = item;
            that.optionName = that.choosetaskmodel.Name + '(' + that.choosetaskmodel.Number + ')';
            this.$refs.choosetask.toggle();
            that.form.TaskStatus = item.Type;
            that.form.pageindex = 0;
            that.form.pagesize = 10;
            that.get_data();
        },
        onChooseSort: function (item) {
            var that = this;
            that.choosesortmodel = item;
            this.$refs.choosesort.toggle();
            that.OrderByType = item.value;
            that.form.pageindex = 0;
            that.form.pagesize = 10;
            that.get_data();
        },
        open_list: function (item) {
            var that = this;
            var url = 'equipment_list_frm';
            var title = item.DevicePlanName;
            ns.openWin(url, title, {
                id: item.ID
            });
        },
        open_select: function () {
            var that = this;
            that.showTop = !that.showTop;
        },
        open_add: function () {
            var that = this;
            ns.openWin('equipment_addfrm', '新增记录');
        },
        doHideSearch: function () {
            var that = this;
            that.showTop = false;
            that.showsearch = false;
        },
        do_closearch: function () {
            var that = this;
            that.showsearch = false;
            that.showTop = false;
            that.showsearchresult = false;
            that.form.planTypeList = [1, 2];
            that.list = [];
            that.get_data();
        },
        select_allstatus: function () {
            var that = this;
            that.showsearch = !that.showsearch;
            // that.form.planTypeList=[1,2];
            var options = {};
            options.planTypeList = JSON.stringify(that.form.planTypeList);
            options.action = "getdeviceplantaskpendinghandlelist";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.selectlist = data.list;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_scrollTo: function () {
            var that = this;
            var sollid = document.getElementById('scroll' + that.form.ID);
            window.scrollTo(0, sollid.offsetTop);
            var classlist = document.getElementById('scroll' + that.form.ID).getElementsByClassName('boxbg')[0];
            classlist.style.backgroundColor = "#eaf1ff";
            setTimeout(function () {
                classlist.style.backgroundColor = "#f7fbff";
            }, 800)
        },
    },

});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.get_menuType();
    app.form.ID = api.pageParam.ID || 0;
    app.get_data();
    // api.addEventListener({
    //     name: 'devicetaskcomplete'
    // }, function(ret, err) {
    //     app.get_data();
    // });
    // api.addEventListener({
    //     name: 'startplantaskdone'
    // }, function(ret, err) {
    //     app.get_data();
    // });
    api.addEventListener({
        name: 'do_open_add'
    }, function (ret, err) {
        app.open_add();
    });
    api.addEventListener({
        name: 'do_open_select'
    }, function (ret, err) {
        app.open_select();
    });
    // ns.init_pull(true,true);
    ns.init_pull();
};

